package dao

import (
	"context"
	"go-shop-admin/internal/model"
)
import "gorm.io/gorm"

type AuthRoleDao struct {
	*gorm.DB
}

var authRoleSelectField = []string{"id", "name", "desc", "status", "sort", "created_at"}

// NewAuthRoleDao 把init init_mysql的Db复制过来
func NewAuthRoleDao(ctx context.Context) *AuthRoleDao {
	return &AuthRoleDao{NewDBClient(ctx)}
}

// AuthRoleAdd 新增
func (d *AuthRoleDao) AuthRoleAdd(role *model.AuthRole) error {
	return d.DB.Model(&model.AuthRole{}).Create(role).Error
}

// GetById 通过id获取用户信息
func (d *AuthRoleDao) GetById(id uint) (object *model.AuthRole, err error) {
	err = d.DB.Model(&model.AuthRole{}).Select(authRoleSelectField).Where("id=?", id).First(&object).Error
	return
}

// AuthRoleUpdate 更新
func (d *AuthRoleDao) AuthRoleUpdate(id uint, role *model.AuthRole) error {
	return d.DB.Model(&model.AuthRole{}).Where("id=?", id).Updates(role).Error
}

// DeleteByIds 删除
func (d *AuthRoleDao) DeleteByIds(ids []string) error {
	return d.DB.Delete(&model.AuthRole{}, ids).Error
}
